package com.atguigu.interview.algo.test;


import java.util.HashMap;
import java.util.Map;

/**
 * @author Jungle
 * @create 2023-09-11 14:58
 */
public class LruCacheDemo02 {
    //map负责查找，构建一个虚拟的双向链表，里面安装的就是一个Node节点
    //1、构造一个Node节点，作为数据载体
    class Node<K, V> {
        K key;
        V value;
        Node<K, V> prev;
        Node<K, V> next;

        public Node() {
            prev = next = null;
        }

        public Node(K key, V value) {
            this.key = key;
            this.value = value;
            prev = next = null;
        }
    }


    //2、构建一个虚拟的双向链列，里面安放的就是我们的Node
    class DoubleLinkedList<K, V> {
        Node<K, V> head;
        Node<K, V> tail;

        //2.1 构造方法
        public DoubleLinkedList() {
            head = new Node<>();
            tail = new Node<>();
            //双向链表
            head.next = tail;
            tail.prev = head;

        }


        //2.2、添加到头
        public void addHead(Node<K, V> node) {
            node.next = head.next;
            node.prev = head;
            head.next.prev = node;
            head.next = node;

        }

        //2.3、删除节点
        public void removeNode(Node<K, V> node) {
            node.next.prev = node.prev;
            node.prev.next = node.next;
            node.prev = null;
            node.next = null;

        }


        //2.4、获得最后一个节点
        public Node getLast() {
            return tail.prev;
        }
    }

    private int cacheSize;

    //添加两个属性
    Map<Integer, Node<Integer, Integer>> map;
    DoubleLinkedList<Integer, Integer> doubleLinkedList;


    //3、构造方法


    public LruCacheDemo02(int cacheSize) {
        this.cacheSize = cacheSize;  //坑位
        map = new HashMap<>(); //查找
        doubleLinkedList = new DoubleLinkedList<>();
    }


    //查找
    public int get(int key) {
        if (!map.containsKey(key)) {
            return -1;
        }
        //    获取到了，删除这个节点，并把当前节点添加到头
        Node<Integer, Integer> node = map.get(key);
        doubleLinkedList.removeNode(node);
        doubleLinkedList.addHead(node);
        return node.value;
    }

    /**
     * 插入
     */
    public void put(int key, int value) {
        //如果有这个数据就更新操作，删除当前节点，并移动到队头
        if (map.containsKey(key)) {
            Node<Integer, Integer> node = map.get(key);
            //更新hash
            node.value = value;
            map.put(key, node);
            doubleLinkedList.removeNode(node);
            doubleLinkedList.addHead(node);
        } else {
            if (map.size() == cacheSize) { //坑位满了
                Node<Integer, Integer> lastNode = doubleLinkedList.getLast();
                map.remove(lastNode.key);
                doubleLinkedList.removeNode(lastNode);
            }
            //新增一个节点
            // 才是新增
            Node<Integer, Integer> newNode = new Node<>(key, value);
            map.put(key, newNode);
            doubleLinkedList.addHead(newNode);
        }
    }

    public static void main(String[] args) {
        LruCacheDemo02 lruCacheDemo = new LruCacheDemo02(3);
        lruCacheDemo.put(1, 1);
        lruCacheDemo.put(2, 2);
        lruCacheDemo.put(3, 3);
        System.out.println(lruCacheDemo.map.keySet());
        lruCacheDemo.put(4, 4);
        System.out.println(lruCacheDemo.map.keySet());
        lruCacheDemo.put(3, 3);
        System.out.println(lruCacheDemo.map.keySet());
        lruCacheDemo.put(3, 3);
        System.out.println(lruCacheDemo.map.keySet());
        lruCacheDemo.put(3, 3);
        System.out.println(lruCacheDemo.map.keySet());
        lruCacheDemo.put(5, 5);
        System.out.println(lruCacheDemo.map.keySet());
    }

}
